<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-06-19 08:07:01
 * @LastEditors: wiz
 * @LastEditTime: 2023-07-04 10:11:47
-->
<!--
 * @Description: 
 
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-05-15 09:40:11
 * @LastEditors: wiz
 * @LastEditTime: 2023-05-19 15:56:23
-->
<!--
 * description: 节点属性
-->
<template>
  <div class="nodeAttr">
    <Collapse v-model="G_selectIdx">
      <Panel name="attr">
        <span class="fountColor">节点属性</span>
        <p slot="content">
          <component
            :is="G_currentRole"
            v-bind="{
              compBoxInfo: this.GT_compBoxInfo,
              params: { disabled: G_disabled, data: G_compData }
            }"
          />
        </p>
      </Panel>
    </Collapse>
  </div>
</template>

<script lang="ts">
import {
  T_srv,
  Tsrv,
  ts,
  ifs,
  beginNode,
  endNode,
  ifNode,
  switchNode as caseNode,
  exprNode,
  nodeBtnList,
  I_ruleEditor,
  I_nodeAttrData,
  I_switch,
  vue
} from './config';

@T_srv.comp({
  components: {
    beginNode,
    endNode,
    ifNode,
    caseNode,
    exprNode
  }
})
export default class nodeArrt extends vue {
  // 父组件compBoxInfo
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  private GT_compBox: ifs.I_compBox;

  private G_compID: string;

  private GT_compBoxInfo: ifs.I_compBoxInfo;

  // 展开
  private G_selectIdx: Array<string> = ['attr'];

  // 当前组件名
  private G_currentRole: string = null;

  // 工具栏 未用
  private G_nodeBtnList: Array<object> = nodeBtnList;

  // 全局不可用状态
  private G_disabled: boolean = true;

  // 动态组件数据
  private G_compData: any = null;

  // 已知组件
  private G_comps: object = {
    beginNode: {
      // 组件在注册compBox的名字
      compName: 'nodeAttr_begin',
      enabled: true
    },
    endNode: {
      compName: 'nodeAttr_end',
      enabled: true
    },
    ifNode: {
      compName: 'nodeAttr_if',
      enabled: true
    },
    caseNode: {
      compName: 'nodeAttr_switch',
      enabled: true
    },
    exprNode: {
      compName: 'nodeAttr_expr',
      enabled: true
    }
  };

  created() {
    this.G_currentRole = '';
    this.G_compID = Tsrv.utils.getGUID();
    // 如果外部有传入组件信息
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox = new ts.T_compBox(this.G_compID); // 使用本地实例
    } else {
      this.G_compID = this.compBoxInfo.compID;
      this.GT_compBox = this.compBoxInfo.TcompBox;
    }
    this.GT_compBoxInfo = { compID: this.G_compID, TcompBox: this.GT_compBox };
  }

  mounted() {
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
    this.GT_compBox.addCompMergeName(this.G_compID, 'nodeAttr', this);
  }

  destroyed() {
    this.GT_compBox.removeCompMergeName(this.G_compID, 'nodeAttr');
  }

  /**
   * @Author: wiz
   * @param {*} compName
   * @return {*}
   * @description: 选择组件
   */

  public selectComp(e: any) {
    this.G_compData = null;
    this.G_currentRole = '';
    if (Tsrv.utils.isEmpty(e)) {
      return;
    }
    setTimeout(() => {
      const L_editor: I_ruleEditor = this.GT_compBox.getCompMergeName(
        this.G_compID,
        `ruleEditor`
      ).G_editor;
      const L_model = e.item.getModel();
      if (this.G_comps[L_model.type].enabled) {
        const L_compData = L_editor.getNodeAttrData(L_model.id);
        if (!L_compData) {
          this.G_compData = { nodeID: L_model.id };
        } else {
          this.G_compData = L_compData;
        }
        if (L_model.type === `caseNode`) {
          // 重装数据
          this.G_compData = this.switchNodeHandle(L_compData, L_model, e.item);
          // 保存数据
          L_editor.setNodeAttrData(this.G_compData);
        }
        this.G_currentRole = L_model.type;
      }
    }, 100);
  }

  public getCurrCompData(): any {
    return this.G_compData;
  }

  /**
   * @Author: wiz
   * @param {*} P_compData
   * @param {*} P_model
   * @param {*} P_item
   * @return {*}
   * @description: 当节点数据处理，实时扫描连线填充数据，如果无源数据则组建数据
   */
  private switchNodeHandle(P_compData: any, P_model: any, P_item: any) {
    const L_data: I_switch = {
      nodeID: P_model.id,
      type: P_model.type,
      nodeName: P_model.name,
      exprs: []
    };
    const L_dataMap = new Map();
    const L_outEdge = P_item.getOutEdges();
    // 重新装填
    L_outEdge.forEach(itm => {
      L_data.exprs.push({
        edgeID: itm.getID(),
        expr: '',
        explain: ''
      });
    });

    if (P_compData) {
      // 构建指针
      P_compData.exprs.forEach(ele => {
        L_dataMap.set(ele.edgeID, ele);
      });
      //  抄旧值
      L_data.exprs.forEach((item, idx) => {
        if (L_dataMap.has(item.edgeID)) {
          L_data.exprs[idx] = L_dataMap.get(item.edgeID);
        }
      });
    }
    return L_data;
  }

  private nodeBtnClick() {}
  /**
   * @Author: wiz
   * @param {*} data
   * @param {*} rowID
   * @return {*}
   * @description: 设置数据
   */

  public setData(data) {
    const L_editor: I_ruleEditor = this.GT_compBox.getCompMergeName(
      this.G_compID,
      `ruleEditor`
    ).G_editor;
    L_editor.nodeAttr = data;
    // 清空组件
    this.G_currentRole = '';
  }
  /**
   * @Author: wiz
   * @param {*} P_s
   * @return {*}
   * @description: 设置状态
   */

  public setCompDisabled(P_s: boolean) {
    this.G_disabled = P_s;
    if (this.G_currentRole !== ``) {
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        this.G_comps[this.G_currentRole].compName
      ).setDisabled(P_s);
    }
  }
}
</script>
<style scoped lang="scss">
.nodeAttr {
  height: 100%;
  width: 100%;
  overflow: auto;
}
.fountColor {
  color: var(--default-hover-font-color);
}
</style>
